package com.qdlc.p2p.manage.action.additional;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.AdditionalCommodityService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.AdditionalCommodity;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.model.AdditionalCommodityModel;

/**
 * Created by liaoxiangkai on 2015/12/1.
 */
public class ManageCommodityAction extends BaseAction<AdditionalCommodityModel> implements ModelDriven<AdditionalCommodityModel> {
    @Resource
    private AdditionalCommodityService additionalCommodityService;

    private Map<String, Object> data;

    /**
     * 商品管理
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/commodity/commodityManage")
    public String commodityManage() throws Exception {
        return "commodityManage";
    }

    @Action(value="/modules/additional/commodity/commodityDetailPage")
    public String commodityDetailPage() {
        AdditionalCommodityModel additionalCommodityModel = additionalCommodityService.findById(model.getId());
        if (null != additionalCommodityModel)
            request.setAttribute("commodity", additionalCommodityModel);
        return "commodityDetailPage";
    }

    /**
     * 商品列表
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/commodity/commodityList")
    public void commodityList() throws Exception {
        PageDataList<AdditionalCommodityModel> dataList = additionalCommodityService.list(model);
        data = new HashMap<String, Object>();
        data.put("total", dataList.getPage().getTotal());
        data.put("rows", dataList.getList());
        printJson(getStringOfJpaObj(data));
    }

    /**
     * 添加商品页面
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/commodity/commodityAddPage")
    public String commodityAddPage() throws Exception {
        return "commodityAddPage";
    }

    /**
     * 添加商品
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/commodity/doCommodityAdd")
    public void doCommodityAdd() throws Exception {
        Operator operator = getOperator();
        if (null == operator) {
            printWebResult("请先登录！", false);
            return;
        }
        if (!checkCommodity(model)){
            printWebResult("必填项未填写！", false);
            return;
        }
        model.setCreator(operator);
        model.setCreateTime(DateUtil.getNow());
        AdditionalCommodity additionalCommodity = additionalCommodityService.save(model);
        //判断是否添加成功
        if (null == additionalCommodity) {
            printWebResult("添加失败！", false);
            return;
        }
        printWebSuccess();
    }

    /**
     * 修改商品页面
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/commodity/commodityEditPage")
    public String commodityEditPage() throws Exception {
        AdditionalCommodityModel additionalCommodityModel = additionalCommodityService.findById(model.getId());
        if (null != additionalCommodityModel)
            request.setAttribute("commodity", additionalCommodityModel);
        return "commodityEditPage";
    }

    /**
     * 修改商品
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/commodity/doCommodityEdit")
    public void doCommodityEdit() throws Exception {
        Operator operator = getOperator();
        if (null == operator) {
            printWebResult("请先登录！", false);
            return;
        }
        if (!checkCommodity(model)){
            printWebResult("必填项未填写！", false);
            return;
        }
        model.setModifier(operator);
        model.setModifyTime(DateUtil.getNow());
        AdditionalCommodity additionalCommodity = additionalCommodityService.update(model);
        if (null == additionalCommodity) {
            printWebResult("更新失败！", false);
            return;
        }
        printWebSuccess();
    }

    /**
     * 禁用商品
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/commodity/commodityDisable")
    public void commodityDisable() throws Exception {
        Operator operator = getOperator();
        if (null == operator) {
            printWebResult("请先登录！", false);
            return;
        }
        model.setModifier(operator);
        model.setModifyTime(DateUtil.getNow());
        int result = additionalCommodityService.toggleStatus(model);
        if (result <= 0) {
            printWebResult("禁用失败！", false);
            return;
        }
        printWebSuccess();
        
    }

    /**
     * 启用商品
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/commodity/commodityEnable")
    public void commodityEnable() throws Exception {
        Operator operator = getOperator();
        if (null == operator) {
            printWebResult("请先登录！", false);
            return;
        }
        model.setModifier(operator);
        model.setModifyTime(DateUtil.getNow());
        int result = additionalCommodityService.toggleStatus(model);
        if (result <= 0) {
            printWebResult("启用失败！", false);
            return;
        }
        printWebSuccess();
    }

    @Action(value = "/modules/additional/commodity/doCommodityDelete")
    public void doCommodityDelete() throws Exception {
        Operator operator = getOperator();
        if (null == operator) {
            printWebResult("请先登录！", false);
            return;
        }
        model.setModifier(operator);
        model.setModifyTime(DateUtil.getNow());
        int result = additionalCommodityService.delete(model);
        if (result <= 0) {
            printWebResult("删除失败！", false);
            return;
        }
        printWebSuccess();
    }

    /**
     * 商品名称和id的json数据
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/commodity/jsonLotteryCommodityList")
    public void jsonLotteryCommodityList() throws Exception {
        if (StringUtil.isBlank(paramString("category"))) {
            return;
        }
        int category = paramInt("category");
        String json = additionalCommodityService.lotteryCommodityList(category);
        //如果查询出来的结果为空
        //if (StringUtil.isNotBlank(json))
            printJson(json);
        //printJson("");
    }



    /**
     * ===================检查=======================
     * 商品添加修改时的检查
     * @param model
     * @return
     */
    private boolean checkCommodity(AdditionalCommodityModel model) {
        //判断填写商品名称
        if (StringUtil.isBlank(model.getName()))
            return  false;
        //判断商品类别
        return !StringUtil.isBlank(model.getCategory());
    }
}
